package cyy.example.com.facotry.net.helper;


import android.text.TextUtils;
import android.util.Log;

import com.example.com.common.factory.data.DataSource;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.FirmGeneralInfoEntity;
import cyy.example.com.facotry.model.api.response.FirmListEntity;
import cyy.example.com.facotry.model.api.response.FirmLocationEntity;
import cyy.example.com.facotry.model.api.response.RealDataEntity;
import cyy.example.com.facotry.model.api.response.VideoInfoEntity;
import cyy.example.com.facotry.net.Network;
import cyy.example.com.facotry.net.RemoteService;
import cyy.example.com.facotry.net.RequestIdFactory;
import retrofit2.Call;

/**
 * Created by chenyiyao on 2017/10/25.
 */

public class FirmNetHelper {

    /**
     * 获取公司列表
     */
    public static Call getSpecialFirm(String companyId, DataSource.Callback<BaseEntity<List<FirmListEntity>>> callback) {
        return getFirmListInfo(1, 1, companyId, "", "", null, null, null, callback);
    }

    /**
     * 获取公司列表
     */
    public static Call searchSpecialFirm(int pageSize, int pageNum, String name, DataSource.Callback<BaseEntity<List<FirmListEntity>>> callback) {
        return getFirmListInfo(pageSize, pageNum, "", name, "", null, null, null, callback);
    }


    /**
     * 获取公司列表
     *
     * @param pageSize        每页的数量
     * @param pageNum         页数
     * @param companyId       公司id
     * @param companyName     公司名称
     * @param smartCdt        智能筛选条件
     * @param businessType    公司类型
     * @param regulationLevel 监管等级
     * @param regulationType  监管性质
     * @param callback        回调
     */
    public static Call getFirmListInfo(int pageSize, int pageNum, String companyId, String companyName,
                                       String smartCdt, List businessType, List regulationLevel, List regulationType,
                                       DataSource.Callback<BaseEntity<List<FirmListEntity>>> callback) {
        Map json = new HashMap();
        Gson gson = new Gson();
        json.put("pageSize", pageSize);
        json.put("pageNum", pageNum);
        if (!TextUtils.isEmpty(companyId)) {
            json.put("companyId", companyId);
        }
        if (!TextUtils.isEmpty(companyName)) {
            json.put("companyName", companyName);
        }
        if (!TextUtils.isEmpty(smartCdt)) {
            json.put("smartCdt", smartCdt);
        }

        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService remote = Network.remote();
        // 得到一个Call
        Call<BaseEntity<List<FirmListEntity>>> call = remote.
                queryFirmList(json,
                        businessType, regulationType, regulationLevel);
        // 异步的请求
        call.enqueue(new RspCallback<>(callback, RequestIdFactory.QUERY_FIRM_LIST_INFO));
        return call;
    }


    //获取公司综合信息
    public static Call getFirmGeneralInfo(String firmId, DataSource.Callback<BaseEntity<FirmGeneralInfoEntity>> callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService remote = Network.remote();
        // 得到一个Call
        Call call = remote.queryFirmGeneralInfo(firmId);
        // 异步的请求
        call.enqueue(new RspCallback<>(callback, RequestIdFactory.GET_RESOURCE_TABLE_DATA));
        return call;
    }

    //公司视频
    public static Call getVideoUrl(String firmId, DataSource.Callback<BaseEntity<VideoInfoEntity>> callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getVideoUrl(firmId);
        call.enqueue(new RspCallback<>(callback, RequestIdFactory.GET_VIDEO_URL));
        return call;
    }


    /**
     * 获取公司站位列表
     *
     * @param firmId 公司id
     */
    public static Call getInstitutionList(String firmId, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getInstitutionList(firmId);
        call.enqueue(new RspCallback(callback, RequestIdFactory.GET_FIRM_INSTITUTION));
        return call;
    }


    /**
     * 获取间隔小时的实时数据
     *
     * @return
     */
//    public static Call getFirmRealHoursData(String siteCode, DataSource.Callback callback) {
//        Calendar time = Calendar.getInstance();
//        time.set(2018, 5, 1, 0, 0, 0);
//        String starTime = TimeUtils.date2String(time.getTime(), new SimpleDateFormat(Constants.REAL_DATA_TIME_FORMAT_TYPE, Locale.getDefault()));
//        return getFirmHistoryData("2", "", "", "", siteCode, callback);
//    }

    /**
     * 获取实时数据
     *
     * @param timeType    时间间隔类型  1分钟  2小时
     * @param beginTime   开始时间
     * @param endTime     结束时间
     * @param companyName 公司名称
     * @param siteCode    站位code
     */
    public static Call getFirmHistoryData(String pageNumbn, String pageSize, String timeType, String beginTime,
                                          String endTime, String companyName, String siteCode, DataSource.Callback callback) {

//        TimeUtils.getMillis(new Date(2018,6,1));
//        TimeUtils.getString()
        Map<String, String> datas = new HashMap();
//        if (!TextUtils.isEmpty(regionCode)) {
//            datas.put("regionCode", regionCode);
//        }
        if (!TextUtils.isEmpty(timeType)) {
            datas.put("timeType", timeType);
        }
        if (!TextUtils.isEmpty(beginTime)) {
            datas.put("beginTime", beginTime);
        }
        if (!TextUtils.isEmpty(endTime)) {
            datas.put("endTime", endTime);
        }
        if (!TextUtils.isEmpty(companyName)) {
            datas.put("companyName", companyName);
        }
        if (!TextUtils.isEmpty(siteCode)) {
            datas.put("siteCode", siteCode);
        }

        datas.put("currentPage", pageNumbn);
        datas.put("pageSize", pageSize);
        RemoteService remote = Network.remote();
        Call call = remote.getRealData(datas);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.GET_FIRM_HISTORY_DATA));
        return call;
    }

    public static Call getAllFirmLocation(DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getAllFirmLocation();
        call.enqueue(new RspCallback<BaseEntity<List<FirmLocationEntity>>>(callback, RequestIdFactory.FIRM_LOCATION));
        return call;
    }


    /**
     * 获取间隔小时的实时数据
     *
     * @return
     */
    public static Call getFirmRealData(String siteCode, String companyName, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map<String, String> jsonMap = new HashMap<>();
        if (!TextUtils.isEmpty(siteCode)) {
            jsonMap.put("siteCode", siteCode);
        }
        if (!TextUtils.isEmpty(companyName)) {
            jsonMap.put("companyId", companyName);
        }
        Call call = remote.getFirmLatestData(jsonMap);
        call.enqueue(new RspCallback(callback, RequestIdFactory.GET_FIRM_REAL_DATA));
        return call;
    }

    /**
     * 获取间隔小时的实时数据
     *
     * @return
     */
    public static Call getUnnormalFacilityInfo(String companyId, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getUnnormalFacilityInfo(companyId);

        call.enqueue(new RspCallback<BaseEntity<RealDataEntity>>(callback, RequestIdFactory.GET_UNNORM_FACILITY));
        return call;
    }

    public static Call getPublishFile(String recId, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getPublishFile(recId);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.GET_PUBLISH_FILE));
        return call;
    }

    public static Call getAverageValue(String avgType, String siteCode,
                                       String beginTime, String endTime, String currentPage, String pageSize, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map map = new HashMap();
        map.put("avgType", avgType);
        map.put("siteCode", siteCode);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("currentPage", currentPage);
        map.put("pageSize", pageSize);
        Call call = remote.getAverageValue(map);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.SEARCH_AVERAGE_VALUE));
        return call;
    }

    public static Call getDevicesList(String componeId, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map map = new HashMap();
        map.put("companyId", componeId);
        Call call = remote.getDevicesList(map);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.FIRM_DEVICES_COUNT));
        return call;
    }
}
